//
//  HotViewController.m
//  GiftSay
//
//  Created by dllo on 16/1/6.
//  Copyright © 2016年 liyinkai. All rights reserved.
//

#import "HotViewController.h"

@interface HotViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong) UICollectionView *collection;
@property (nonatomic,strong) NSMutableArray *itemsArray;
@property (nonatomic,assign) NSInteger number;

@end

@implementation HotViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"热门";
    
    self.number = 0;
    
    self.view.backgroundColor = [UIColor colorWithWhite:0.7 alpha:1];
    
    self.itemsArray = [NSMutableArray arrayWithCapacity:0];
    
    [self getaDatource];
    [self createdCollectionview];
    
    // Do any additional setup after loading the view.
}

#pragma mark 刷新
- (void)reload
{
    NSLog(@"刷新");
    if (self.itemsArray.count > 0) {
        [self.itemsArray removeAllObjects];
    }
    
    [self getaDatource];
}

#pragma mark 加载
- (void)addItem
{
    NSLog(@"加载");
    
    self.number += 1;
    [self getaDatource];
}

#pragma mark 本页缓存
- (void)cacheItems
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    
    NSString *cachesDirectory = [paths lastObject];

    cachesDirectory = [cachesDirectory stringByAppendingString:@"/HotItemsCache"];
    
    [self.itemsArray writeToFile:cachesDirectory atomically:YES];
    
    NSLog(@"%@",cachesDirectory);
    
}

//- (void)viewWillDisappear:(BOOL)animated
//{
//    [self cacheItems];
//}

//创建collectionview

- (void)createdCollectionview
{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.sectionInset = UIEdgeInsetsMake(kWidth / 50, kWidth / 50, kWidth/50, kWidth/50);
    
    flow.minimumInteritemSpacing = kWidth / 50;
    flow.minimumLineSpacing = kWidth / 50;
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.size.width, self.view.size.height - 64) collectionViewLayout:flow];
    self.collection.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    
    [self.collection registerClass:[BaseCollectionViewCell class] forCellWithReuseIdentifier:@"BaseCollectionViewCell"];
    
    self.collection.delegate = self;
    self.collection.dataSource = self;
    
    [self.view addSubview:self.collection];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(kWidth / 50 * 23, kHeight / 10 * 4);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return  self.itemsArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BaseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BaseCollectionViewCell" forIndexPath:indexPath];
    if (self.itemsArray.count > indexPath.row) {
        cell.item = self.itemsArray[indexPath.row];
    }
    
    return cell;
}

#pragma mark 监听collection位置
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    if (scrollView.contentOffset.y > 0 || scrollView.contentOffset.y < -64){
//        NSLog(@"%f",scrollView.contentOffset.y);
//        [self.collection setContentOffset:CGPointMake(0, scrollView.contentOffset.y)];
//        
//        NSLog(@"%f",self.collection.contentOffset.y);
//    }
//    else{
//        [self.collection setContentSize:CGSizeMake(0, -64 - 64)];
//    }
//    
////    else if (scrollView.contentOffset.y > 0 && self.collection.contentOffset.y > (kHeight / 10 * 4 + kWidth / 50)* (self.itemsArray.count - 1) / 2 )
////    {
////        NSLog(@"dibu");
////    }
//}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    Items *item = self.itemsArray[indexPath.row];
    
    WebViewController *web = [[WebViewController alloc] init];
    web.url = [NSString stringWithFormat:@"%@%@",kItemsInfo,item.ID];
    //可拼接web
//    web.url = [NSString stringWithFormat:@"%@%@",kItemsInfoTow,item.ID];
//    web.type = @"itemshtml";
    
    [self.navigationController pushViewController:web animated:YES];
    
}

//获取数据
- (void)getaDatource
{
    [LYKNetWorkTool getWithUrl:[NSString stringWithFormat:@"%@%ld",kHotItemsUrl,self.number * 50] parameter:nil httpHeader:nil resposeType:ResponseTypeDATA success:^(id result) {
        id any = [NSJSONSerialization JSONObjectWithData:result options:0 error:nil];
        
        NSDictionary *dicforData = [NSDictionary dictionaryWithDictionary:any[@"data"]];
        
        NSArray *items = [NSArray arrayWithArray:dicforData[@"items"]];
        
        for (NSDictionary *datasource in items) {
            
            NSDictionary *dic = datasource[@"data"];
            Items *item = [[Items alloc] init];
            item.like = [NSString stringWithFormat:@"%@",dic[@"favorites_count"]];
            item.ID = [NSString stringWithFormat:@"%@",dic[@"id"]];
            item.cover_icon = dic[@"cover_image_url"];
            item.title = dic[@"name"];
            item.price = [NSString stringWithFormat:@"%@",dic[@"price"]];
            
            [self.itemsArray addObject:item];
        }
        
        NSLog(@"%ld",self.itemsArray.count);
        
        [self.collection reloadData];
        
    } fail:^(NSError *erro) {
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
